const vm = new Vue({
    el: '#main',
    data: {
        imgUrl: '../image/id_card/id-card-f.jpg',
        imgFile: null,
    },
});

const vmFooter = new Vue({
    el: 'div.footer',
    data: {
        uploadedImg: false,
    },
});

function main() {
    fxj.fixLayout();
    listenFile();
}

function apiready() {
    main();
}

function uploadIdCard() {
    document.getElementById('idCard').click();
}

function listenFile() {
    const fileContainer = document.getElementById('idCard');
    fileContainer.addEventListener('change', async e => {
        const file = e.target.files.length > 0 ? e.target.files[0] : null;
        if (file) {
            vm.imgFile = file;
            vm.imgUrl = await fxj.readFile(file);
            vmFooter.uploadedImg = true;
        }
    });
}

async function submit() {
    const close = fxj.dialog.loading({
        des: '正在上传图片,请稍后',
    });
    const formData = new FormData();
    formData.append('imgFile', vm.imgFile);
    formData.append('token', localStorage.getItem('token'));
    formData.append('order', 1);
    let res = await fetch(`${fxj.serverAddress}home/appUploadImg/uploadIdCardImg`, {
        method: 'POST',
        body: formData,
    });
    close();
    try {
        res = await res.json();
        toast(res.msg);
        if (res.err === 0) {
            setTimeout(() => {
                next();
            }, 1000);
        }
    } catch (e) {
        toast('解析json错误');
    }
}

function next() {
    goPage('upload_idcard.html');
}